package com.whmnx.shop.ui.viewmodel;

import android.app.Application;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.whmnx.base.bean.AddressBean;
import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.bean.OrderBean;
import com.whmnx.base.bean.ShopListBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.router.RouterActivityPath;
import com.whmnx.shop.R;
import com.whmnx.shop.databinding.FragmentConfirmOrder3Binding;
import com.whmnx.shop.ui.activity.ConfirmOrderActivity2;
import com.whmnx.shop.ui.adapter.OrderListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * author : huang
 * date   : 2020/10/19 0019 10:32
 * version: 1.0
 * @author Administrator
 */
public class ConfirmOrderModel2 extends ToolbarViewModel{
    public ObservableField<String> address = new ObservableField<>("");
    public ObservableField<String> totalAmount = new ObservableField<>("");
    public ObservableField<String> remark = new ObservableField<>("");
    public ObservableField<String> redBagMoney = new ObservableField<>("");

    private List<OrderBean.ShopModelBean.ProductListBean> productList;
    private ConfirmOrderActivity2 mFragment;
    private FragmentConfirmOrder3Binding mBinding;
    private OrderListAdapter mOrderListAdapter;
    private OrderBean mOrderBean;
    private AddressBean addressBean;
    private int payType;
    private boolean flag;
    private List<OrderBean.ShopModelBean> shopModel;


    public ConfirmOrderModel2(@NonNull Application application) {
        super(application);
    }

    private Disposable subscribe;

    public void initToolbar(ConfirmOrderActivity2 confirmOrderFragment, FragmentConfirmOrder3Binding binding, ArrayList<String> list) {
        this.mFragment = confirmOrderFragment;
        this.mBinding = binding;
        setTitleText("确认订单");
        initView();
        initData(list);
    }



    @Override
    public void onCreate() {
        super.onCreate();
         selectType(0);
    }

    private void selectType(int type) {
        payType = type;
        switch (type){
            case 0:
                mBinding.ivWxSelect.setImageResource(R.mipmap.check1);
                mBinding.ivZfbSelect.setImageResource(R.mipmap.check2);
                break;
            case 1:
                mBinding.ivWxSelect.setImageResource(R.mipmap.check2);
                mBinding.ivZfbSelect.setImageResource(R.mipmap.check1);
                break;
            default:
        }
    }

    private void initData(ArrayList<String> list) {
        Map<String, Object> params = new HashMap<>();
        params.put("ids",list);
        params.put("userInfo_Id",SPUtils.getInstance().getString(Constants.USERID));

        HttpClient.post(mFragment, Constants.SHOPCARTORDER, params,new CallBack<OrderBean>() {
            @Override
            public void onSuccess(OrderBean orderBean) {
                mOrderBean = orderBean;
                shopModel = orderBean.getShopModel();
                mBinding.rvShop.setLayoutManager(new LinearLayoutManager(mFragment));
                mBinding.rvShop.setItemAnimator(new DefaultItemAnimator());
                mOrderListAdapter = new OrderListAdapter(mFragment);
                mBinding.rvShop.setAdapter(mOrderListAdapter);
                mOrderListAdapter.addAll(shopModel);

                totalAmount.set(orderBean.getTotalAmount() + "");
                redBagMoney.set("¥ 0");
            }
        });
    }

    private void initView() {
        subscribe = RxBus.getDefault().toObservable(Object.class)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object object){
                        List<String> list = new ArrayList<>();
                        if (object.getClass() == list.getClass()){
                            List<String> list2 = (List<String>) object;

                        }else if (AddressBean.class.isInstance(object)){
                            flag = true;
                            addressBean = (AddressBean)object;
                            mBinding.includeAddress.setVisibility(View.VISIBLE);
                            TextView tvName = mBinding.includeAddress.findViewById(R.id.tv_name);
                            TextView tvPhone = mBinding.includeAddress.findViewById(R.id.tv_phone);
                            TextView tvAddress = mBinding.includeAddress.findViewById(R.id.tv_address);
                            tvName.setText("收货人: " + addressBean.getAddress_Name());
                            tvPhone.setText(addressBean.getAddress_Mobile());
                            tvAddress.setText("收货地址: "  + addressBean.getAddress_Detailed());
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }



    //添加地址
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ARouter.getInstance().build(RouterActivityPath.User.PAGER_USERDETAIL).navigation();
        }
    });

    public BindingCommand zfOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            selectType(0);
        }
    });

    public BindingCommand  wxOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            selectType(1);
        }
    });


    //提交订单
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (!flag){
                ToastUtils.showShort("请选添加地址");
                return;
            }
            List<ShopListBean> shopListBean = new ArrayList<>();
            List<ShopListBean.ProductListBean> productListBean = new ArrayList<>();
            productListBean.add(new ShopListBean.ProductListBean());

            productListBean.get(0).setProduct_Id(productList.get(0).getProduct_Id());
            productListBean.get(0).setProduct_Name(productList.get(0).getProduct_Name());
            productListBean.get(0).setProduct_Image(productList.get(0).getProduct_Image());
            productListBean.get(0).setProduct_SPEC(productList.get(0).getProduct_SPEC());
            productListBean.get(0).setProduct_Price(productList.get(0).getProduct_Price() + "");
            productListBean.get(0).setProduct_Num(productList.get(0).getProduct_Num());
            shopListBean.add(new ShopListBean());

            ShopListBean.AddressBean2 addressBean2 = new ShopListBean.AddressBean2();
            addressBean2.setAddress_Id(addressBean.getAddress_Id());
            addressBean2.setAddress_Name(addressBean.getAddress_Name());
            addressBean2.setAddress_Mobile(addressBean.getAddress_Mobile());
            addressBean2.setAddress_Detailed(addressBean.getAddress_Detailed());
            addressBean2.setAddress_Label(addressBean.getAddress_Label());

            shopListBean.get(0).setProductList(productListBean);
            shopListBean.get(0).setMId(mOrderBean.getUserInfo_Id());
            shopListBean.get(0).setRedBagMoney(0 + "");
            shopListBean.get(0).setRemark(remark.get());

            Map<String, Object> params = new HashMap<>();
            params.put("address",addressBean2);
            params.put("shopModel",shopListBean);
            params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
            params.put("isGroup", 0);
            params.put("totalAmount", mOrderBean.getTotalAmount());
            params.put("payType", payType + 1);

            HttpClient.post(mFragment, Constants.ADDORDER, params,new CallBack<LoginBean>() {
                @Override
                public void onSuccess(LoginBean result) {
                    ToastUtils.showShort("提交成功");
                    finish();
                }
            });
        }
    });

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxSubscriptions.remove(subscribe);
    }

}
